#include "mypwm.h"
#include "myhall.h"
#include <string.h>
#include <stdio.h>
#include "main.h"

#define BLIND_START_STEPS 8      // 盲走换向步数
#define STEP_DELAY_MS    5      // 每拍延时(ms)
const uint8_t steps[6] = {2, 6, 4, 5, 1, 3};


//bool PreRotate_Start(uint8_t dir, uint16_t duty, uint8_t temp_hall)
//{
//    for (uint8_t i = 0; i < BLIND_START_STEPS; i++)
//    {
//        uint8_t step;
//        if (dir == 0)  // 顺时针
//            step = steps[i % 6];
//        else           // 逆时针
//            step = steps[5 - (i % 6)];
//        if (dir == 0)
//            UpdateMos_CW(step, duty);
//        else
//            UpdateMos_CCW(step, duty);
//
//        HAL_Delay(STEP_DELAY_MS);
//
//        // 盲走期间检测霍尔信号是否变化
//        if ((Hall_Get_State())!=temp_hall)
//        {
//            // 霍尔信号开始变化，说明转子已经转动，切换闭环控制
//            return true; // 启动成功
//        }
//    }
//
//    // 盲走换向结束，霍尔信号无变化，启动失败
//    return false;
//}
/**
 * @description: 
 * @param dir		电机转动方向	0--CW   1--CCW
 * @param duty	占空比
 * @return {*}
 */
void StartMotor(const uint8_t dir, const uint16_t duty)
{
	__HAL_TIM_ENABLE_IT(&htim1, TIM_IT_COM);
	HAL_TIMEx_HallSensor_Start(&htim2);					  	//开启接口定时器

	  // 再手动触发 COM 中断一次，开始正常换向
	  HAL_TIM_GenerateEvent(&htim1, TIM_EVENTSOURCE_COM);

//	if(dir == 0)
//	{
//		CW_Start_Once(duty);
//	}
//	else
//	{
//		CCW_Start_Once(duty);
//	}

}

void StopMotor(void)
{
	__HAL_TIM_DISABLE_IT(&htim1, TIM_IT_COM);
	HAL_TIMEx_HallSensor_Stop(&htim2);
	HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_1);
	HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_2);
	HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_3);
	HAL_TIMEx_PWMN_Stop(&htim1,TIM_CHANNEL_1);
	HAL_TIMEx_PWMN_Stop(&htim1,TIM_CHANNEL_2);
	HAL_TIMEx_PWMN_Stop(&htim1,TIM_CHANNEL_3);
	memset(TIM1_DMA_Buffer, 0, sizeof(TIM1_DMA_Buffer));
	Motor_Vars.Motor_ActuralSpeed = 0;
}

void UpdateMos_CW(uint8_t hall_value, uint16_t duty , uint8_t motor_dir)
{
	uint8_t Step = hall_value;
	uint16_t temp_duty = duty;

	if(motor_dir==0)
	{
		switch (Step) {
	            case 2:
	                Step = 6;
	                break;
	            case 6:
	                Step = 4;
	                break;
	            case 4:
	                Step = 5;
	                break;
	            case 5:
	                Step = 1;
	                break;
	            case 1:
	                Step = 3;
	                break;
	            case 3:
	                Step = 2;
	                break;
	            default:
	                // Optional: Handle unexpected cases if needed
	                break;
	        }
	}
	else
	{
		switch (Step) {
	            case 2:
	                Step = 4;
	                break;
	            case 6:
	                Step = 5;
	                break;
	            case 4:
	                Step = 1;
	                break;
	            case 5:
	                Step = 3;
	                break;
	            case 1:
	                Step = 2;
	                break;
	            case 3:
	                Step = 6;
	                break;
	            default:
	                // Optional: Handle unexpected cases if needed
	                break;
	        }
	}

	switch(Step)
	{
		case	1:	//U+W-
			PWM_Start_ALL();
			htim1.Instance->CCR1 = temp_duty;
			htim1.Instance->CCR3 = 0;
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);	
			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_2);
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);		
			break;
		case	2:	//V+U-
			PWM_Start_ALL();
			htim1.Instance->CCR2 = temp_duty;
			htim1.Instance->CCR1 = 0;
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);	
			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_3);
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);	
			break;
		case	3:	//V+W-
			PWM_Start_ALL();
			htim1.Instance->CCR2 = temp_duty;
			htim1.Instance->CCR3 = 0;
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);	
			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_1);
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);	
			break;
		case	4:	//W+V-
			PWM_Start_ALL();
			htim1.Instance->CCR3 = temp_duty;
			htim1.Instance->CCR2 = 0;
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);	
			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_1);
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);	
			break;
		case	5:	//U+V-
			PWM_Start_ALL();
			htim1.Instance->CCR1 = temp_duty;
			htim1.Instance->CCR2 = 0;
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);	
			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_3);
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);	
			break;
		case	6:	//W+U-
			PWM_Start_ALL();
			htim1.Instance->CCR3 = temp_duty;
			htim1.Instance->CCR1 = 0;
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);	
			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_2);
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);	
			break;
		default:
			break;
	}
}

//void UpdateMos_CCW(uint8_t hall_value, uint16_t duty)
//{
//	uint8_t Step = hall_value;
//	uint16_t temp_duty = duty;
//
//	switch (Step) {
//            case 3:
//                Step = 1;
//                break;
//            case 1:
//                Step = 5;
//                break;
//            case 5:
//                Step = 4;
//                break;
//            case 4:
//                Step = 6;
//                break;
//            case 6:
//                Step = 2;
//                break;
//            case 2:
//                Step = 3;
//                break;
//            default:
//                // Optional: Handle unexpected cases if needed
//                break;
//        }
//		switch(Step)
//    {
//    	case	1:	//W+U-
//			PWM_Start_ALL();
//			htim1.Instance->CCR1 = 0;
//			htim1.Instance->CCR3 = temp_duty;
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_2);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);
//			break;
//		case	2:	//U+V-
//			PWM_Start_ALL();
//			htim1.Instance->CCR1 = temp_duty;
//			htim1.Instance->CCR2 = 0;
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_3);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);
//			break;
//		case	3:	//W+V-
//			PWM_Start_ALL();
//			htim1.Instance->CCR3 = temp_duty;
//			htim1.Instance->CCR2 = 0;
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_1);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);
//			break;
//		case	4:	//V+W-
//			PWM_Start_ALL();
//			htim1.Instance->CCR2 = temp_duty;
//			htim1.Instance->CCR3 = 0;
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_1);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);
//			break;
//		case	5:	//V+U-
//			PWM_Start_ALL();
//			htim1.Instance->CCR2 = temp_duty;
//			htim1.Instance->CCR1 = 0;
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_3);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);
//			break;
//
//		case	6:	//U+W-
//			PWM_Start_ALL();
//			htim1.Instance->CCR1 = temp_duty;
//			htim1.Instance->CCR3 = 0;
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_2);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);
//			break;
//		default:
//			break;
//	}
//}
#if 0
void UpdateMos_CW(uint8_t hall_value, uint16_t duty)
{
	uint8_t Step = hall_value;
	uint16_t temp_duty = duty;

	switch (Step) {
            case 2:
                Step = 6;
                break;
            case 6:
                Step = 4;
                break;
            case 4:
                Step = 5;
                break;
            case 5:
                Step = 1;
                break;
            case 1:
                Step = 3;
                break;
            case 3:
                Step = 2;
                break;
            default:
                // Optional: Handle unexpected cases if needed
//								debugval++;
                break;
        }
//	debugval = Step;
	switch(Step)
	{
		case	1:	//U+W-
			HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
			HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_3);
			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_2);
	
			htim1.Instance->CCR1 = temp_duty;
			htim1.Instance->CCR3 = 0;
		
			HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3);	
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);
			break;
//		case	2:	//V+U-
			PWM_Stop_ALL();
			htim1.Instance->CCR2 = temp_duty;
			htim1.Instance->CCR1 = 0;
			htim1.Instance->CCR3 = 0;
			PWM_Start_ALL();
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);
			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_3);
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);
			break;
//		case	3:	//V+W-
//			PWM_Stop_ALL();
//			htim1.Instance->CCR2 = temp_duty;
//			htim1.Instance->CCR1 = 0;
//			htim1.Instance->CCR3 = 0;
//			PWM_Start_ALL();
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);	
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_1);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);	
//			break;
		case	4:	//W+V-
			HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);
			HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_3);
			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_1);
			
			htim1.Instance->CCR3 = temp_duty;
			htim1.Instance->CCR2 = 0;
		
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);	
			HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);
			break;
		case	5:	//U+V-
			HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
			HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);
			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_3);
		
			htim1.Instance->CCR1 = temp_duty;
			htim1.Instance->CCR2 = 0;		
		
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);	
			HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);	
			break;
//		case	6:	//W+U-
//			PWM_Stop_ALL();
//			htim1.Instance->CCR3 = temp_duty;
//			htim1.Instance->CCR1 = 0;
//			htim1.Instance->CCR2 = 0;
//			PWM_Start_ALL();
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);	
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_2);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);	
//			break;
//		default:

//			break;
	}
}
#endif

#if 0
void UpdateMos_CCW(uint8_t hall_value, uint16_t duty)
{
	uint8_t Step = hall_value;
	uint16_t temp_duty = duty;

	switch (Step) {
            case 3:
                Step = 1;
                break;
            case 1:
                Step = 5;
                break;
            case 5:
                Step = 4;
                break;
            case 4:
                Step = 6;
                break;
            case 6:
                Step = 2;
                break;
            case 2:
                Step = 3;
                break;
            default:
                // Optional: Handle unexpected cases if needed
//						printf("PhaseNumberError!\r\n");
                break;
        }
		switch(Step)
    {
    	case	1:	//W+U-
			PWM_Start_ALL();
			htim1.Instance->CCR1 = 0;
			htim1.Instance->CCR2 = 0;
			htim1.Instance->CCR3 = temp_duty;
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);		
			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_2);
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);		
			break;
		case	2:	//U+V-
			PWM_Start_ALL();
			htim1.Instance->CCR1 = temp_duty;
			htim1.Instance->CCR2 = 0;
			htim1.Instance->CCR3 = 0;
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);		
			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_3);
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);	
			break;			
		case	3:	//W+V-
			PWM_Start_ALL();
			htim1.Instance->CCR3 = temp_duty;
			htim1.Instance->CCR2 = 0;
			htim1.Instance->CCR1 = 0;
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);		
			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_1);
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);	
			break;
		case	4:	//V+W-
			PWM_Start_ALL();
			htim1.Instance->CCR2 = temp_duty;
			htim1.Instance->CCR3 = 0;
			htim1.Instance->CCR1 = 0;
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);		
			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_1);
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);	
			break;
		case	5:	//V+U-
			PWM_Start_ALL();
			htim1.Instance->CCR2 = temp_duty;
			htim1.Instance->CCR1 = 0;
			htim1.Instance->CCR3 = 0;
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);		
			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_3);
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);	
			break;
			
		case	6:	//U+W-
			PWM_Start_ALL();
			htim1.Instance->CCR1 = temp_duty;
			htim1.Instance->CCR3 = 0;
			htim1.Instance->CCR2 = 0;
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);		
			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_2);
			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);	
			break;
		default:
			break;
	}
}

#endif
/**
 * @description: 定时器通道模式设置
 * @param {TIM_HandleTypeDef } htim	定时器句柄指针
 * @param {uint32_t } Channel		定时器通道
 * @param {uint32_t } Channel		通道模式
 * @return {*}
 */
void TIM_OCMode_Set(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t OCMode)
{
    uint32_t tmpccmrx;

	switch(Channel)
	{
		case TIM_CHANNEL_1:
			tmpccmrx = htim->Instance->CCMR1;
			tmpccmrx &= ~TIM_CCMR1_OC1M;
			tmpccmrx |= OCMode;
			htim->Instance->CCMR1 = tmpccmrx;
			break;
		case TIM_CHANNEL_2:
			tmpccmrx = htim->Instance->CCMR1;
		    tmpccmrx &= ~TIM_CCMR1_OC2M;
			tmpccmrx |= OCMode << 8;
			htim->Instance->CCMR1 = tmpccmrx;
			break;
		case TIM_CHANNEL_3:
			tmpccmrx = htim->Instance->CCMR2;
		    tmpccmrx &= ~TIM_CCMR2_OC3M;
			tmpccmrx |= OCMode;
			htim->Instance->CCMR2 = tmpccmrx;
			break;
		case TIM_CHANNEL_4:
			tmpccmrx = htim->Instance->CCMR2;
			tmpccmrx &= ~TIM_CCMR2_OC4M;
			tmpccmrx |= OCMode << 8;
			htim->Instance->CCMR2 = tmpccmrx;
			break;
		default:
			break;
	}	
}


void CW_Start_Once(uint16_t duty)
{
//	uint8_t Step = Hall_Get_State();
//	uint16_t temp_duty = duty;
//	TIM6->CNT = 0;
//	switch (Step) {
//            case 3:
//                Step = 4;
//                break;
//            case 1:
//                Step = 6;
//                break;
//            case 6:
//                Step = 1;
//                break;
//            case 4:
//                Step = 3;
//                break;
//            case 5:
//                Step = 2;
//                break;
//            case 2:
//                Step = 5;
//                break;
//            default:
//                break;
//        }
//
//	switch(Step)
//	{
//		case	1:	//U+W-
//			PWM_Start_ALL();
//			htim1.Instance->CCR1 = temp_duty;
//			htim1.Instance->CCR2 = 0;
//			htim1.Instance->CCR3 = 0;
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_2);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);
//			break;
//		case	2:	//V+U-
//			PWM_Start_ALL();
//			htim1.Instance->CCR2 = temp_duty;
//			htim1.Instance->CCR1 = 0;
//			htim1.Instance->CCR3 = 0;
//
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_3);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);
//			break;
//		case	3:	//V+W-
//			PWM_Start_ALL();
//			htim1.Instance->CCR2 = temp_duty;
//			htim1.Instance->CCR1 = 0;
//			htim1.Instance->CCR3 = 0;
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_1);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);
//			break;
//		case	4:	//W+V-
//			PWM_Start_ALL();
//			htim1.Instance->CCR3 = temp_duty;
//			htim1.Instance->CCR1 = 0;
//			htim1.Instance->CCR2 = 0;
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_1);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);
//			break;
//		case	5:	//U+V-
//			PWM_Start_ALL();
//			htim1.Instance->CCR1 = temp_duty;
//			htim1.Instance->CCR2 = 0;
//			htim1.Instance->CCR3 = 0;
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_3);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);
//			break;
//		case	6:	//W+U-
//			PWM_Start_ALL();
//			htim1.Instance->CCR3 = temp_duty;
//			htim1.Instance->CCR1 = 0;
//			htim1.Instance->CCR2 = 0;
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_2);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);
//			break;
//		default:
//			break;
//	}
//	HAL_TIM_GenerateEvent(&htim1, TIM_EVENTSOURCE_COM);
	HAL_TIM_GenerateEvent(&htim1, TIM_EVENTSOURCE_COM);
}

void CCW_Start_Once(uint16_t duty)
{
//	uint8_t Step = Hall_Get_State();
//	uint16_t temp_duty = duty;
//	TIM6->CNT = 0;
//
//	switch (Step) {
//            case 3:
//                Step = 4;
//                break;
//            case 1:
//                Step = 6;
//                break;
//            case 6:
//                Step = 1;
//                break;
//            case 4:
//                Step = 3;
//                break;
//            case 5:
//                Step = 2;
//                break;
//            case 2:
//                Step = 5;
//                break;
//            default:
//                break;
//        }
//	switch(Step)
//    {
//    	case	1:	//W+U-
//			PWM_Start_ALL();
//			htim1.Instance->CCR1 = 0;
//			htim1.Instance->CCR2 = 0;
//			htim1.Instance->CCR3 = temp_duty;
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_2);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);
//			break;
//		case	2:	//U+V-
//			PWM_Start_ALL();
//			htim1.Instance->CCR1 = temp_duty;
//			htim1.Instance->CCR2 = 0;
//			htim1.Instance->CCR3 = 0;
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_3);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);
//			break;
//		case	3:	//W+V-
//			PWM_Start_ALL();
//			htim1.Instance->CCR3 = temp_duty;
//			htim1.Instance->CCR2 = 0;
//			htim1.Instance->CCR1 = 0;
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_1);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);
//			break;
//		case	4:	//V+W-
//			PWM_Start_ALL();
//			htim1.Instance->CCR2 = temp_duty;
//			htim1.Instance->CCR3 = 0;
//			htim1.Instance->CCR1 = 0;
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_1);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);
//			break;
//		case	5:	//V+U-
//			PWM_Start_ALL();
//			htim1.Instance->CCR2 = temp_duty;
//			htim1.Instance->CCR1 = 0;
//			htim1.Instance->CCR3 = 0;
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_3);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_3);
//			break;
//
//		case	6:	//U+W-
//			PWM_Start_ALL();
//			htim1.Instance->CCR1 = temp_duty;
//			htim1.Instance->CCR3 = 0;
//			htim1.Instance->CCR2 = 0;
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_1);
//			HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_2);
//			HAL_TIMEx_PWMN_Stop(&htim1, TIM_CHANNEL_2);
//			break;
//		default:
//			break;
//	}
//	HAL_TIM_GenerateEvent(&htim1, TIM_EVENTSOURCE_COM);
	HAL_TIM_GenerateEvent(&htim1, TIM_EVENTSOURCE_COM);

}
void PWM_Start_ALL(void)
{
	HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
	HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);
	HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_3);
	HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_1);
	HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_2);
	HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_3);
}
void PWM_Stop_ALL(void)
{
	HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_1);
	HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_2);
	HAL_TIM_PWM_Stop(&htim1,TIM_CHANNEL_3);
//	HAL_TIMEx_PWMN_Stop(&htim1,TIM_CHANNEL_1);
//	HAL_TIMEx_PWMN_Stop(&htim1,TIM_CHANNEL_2);
//	HAL_TIMEx_PWMN_Stop(&htim1,TIM_CHANNEL_3);
}

void Fast_Stop_Motor(uint16_t duty)
{
	uint16_t temp_duty = duty;
	PWM_Start_ALL();
	htim1.Instance->CCR1 = temp_duty;
	htim1.Instance->CCR2 = temp_duty;
	htim1.Instance->CCR3 = temp_duty;
	PWM_Stop_ALL();
//	__HAL_TIM_DISABLE_IT(&htim1, TIM_IT_COM);
//	HAL_TIMEx_HallSensor_Stop(&htim2);
}

void Brake_Stop_Motor(void)
{
	PWM_Start_ALL();
	htim1.Instance->CCR1 = 5665;
	htim1.Instance->CCR2 = 5665;
	htim1.Instance->CCR3 = 5665;
//	htim1.Instance->CCR1 = (uint16_t)(5665/2);
//	htim1.Instance->CCR2 = (uint16_t)(5665/2);
//	htim1.Instance->CCR3 = (uint16_t)(5   665/2);
	PWM_Stop_ALL();
	__HAL_TIM_DISABLE_IT(&htim1, TIM_IT_COM);
	HAL_TIMEx_HallSensor_Stop(&htim2);
	Motor_Vars.Motor_BrakeFlag = 1;
}
